package com.indexingsystem.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.ExportPahtUpdateReqVo;
import com.indexingsystem.boss.Vo.ExportPathAddReqVo;
import com.indexingsystem.boss.Vo.ExportPathReqVo;
import com.indexingsystem.boss.admin.mapper.IdxExportPathMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.IdxExportPath;
import com.indexingsystem.boss.entity.IdxExportPathExample;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.exception.ServiceResultException;
import com.indexingsystem.system.service.IdxExportPathService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class IdxExportPathServiceImpl implements IdxExportPathService {

    @Resource
    private IdxExportPathMapper idxExportPathMapper;

    @Override
    public int insertSelective(ExportPathAddReqVo reqVo) throws ServiceResultException{

        String pathCode = UUID.randomUUID().toString().replaceAll("-", "");
        IdxExportPath record = new IdxExportPath(pathCode,reqVo.getPathName(),reqVo.getPath(),reqVo.getStatus(),reqVo.getCreateBy(),new Date(),reqVo.getDescription());
        return idxExportPathMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(ExportPahtUpdateReqVo reqVo) throws StringIndexOutOfBoundsException{

        IdxExportPath record = new IdxExportPath(reqVo.getPathCode(),reqVo.getPathName(),reqVo.getPath(),reqVo.getStatus(),reqVo.getCreateBy(),null,reqVo.getDescription());
        return idxExportPathMapper.updateByPrimaryKeySelective(record);

    }
    @Override
    public Integer selectByPrimaryKeyList(List<String> list) {
        return idxExportPathMapper.selectByPrimaryKeyList(list);
    }

    @Override
    public Integer selectByStatusOk(List<String> list){
        return idxExportPathMapper.selectByStatusOk(list);
    };


    @Override
    public int deleteByPathCodeList(List<String> list) throws ServiceResultException{

        return idxExportPathMapper.deleteByPrimaryKeys(list);
    }

    @Override
    public PagedResult<IdxExportPath> getExportPathList(ExportPathReqVo reqVo) throws ParseException {
        Integer pageNo = reqVo.getPageNo() == null ? 1 : reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize() == null ? 20 : reqVo.getPageSize();
        String startTimeStr = reqVo.getStartTime();
        String endTimeStr = reqVo.getEndTime();
        String queryName = reqVo.getQueryCriteria();
        IdxExportPathExample example = new IdxExportPathExample();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date value1 = new Date();
        Date value2 = new Date();
        IdxExportPathExample.Criteria criteria = example.createCriteria();
        if (queryName != null) {
            criteria.andPathNameLike("%" + queryName + "%");
        }
        if ( StringUtils.isNotBlank(startTimeStr)) {
            value1 = df.parse(startTimeStr);
            criteria.andCreateTimeGreaterThanOrEqualTo(value1);
        }
        if (StringUtils.isNotBlank(endTimeStr)) {
            value2 = df.parse(endTimeStr);
            criteria.andCreateTimeLessThanOrEqualTo(value2);
        }
        example.getOredCriteria().add(criteria);
        //startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNo,pageSize);
        return BeanUtils.toPagedResult(idxExportPathMapper.selectByExampleWithBLOBs(example));
    }
}
